package com.zzz.robot201.app.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.base.delegate.IActivity;
import com.jess.arms.integration.cache.Cache;
import com.jess.arms.integration.cache.CacheType;
import com.jess.arms.integration.lifecycle.ActivityLifecycleable;
import com.jess.arms.mvp.IPresenter;
import com.jess.arms.utils.ArmsUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.zzz.robot201.widget.dialog.LoadingDialog;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;

import static com.jess.arms.utils.ThirdViewUtil.convertAutoView;

public abstract class BaseViewActivity <P extends IPresenter> extends AppCompatActivity implements IActivity, ActivityLifecycleable, NetworkUtils.OnNetworkStatusChangedListener {

    protected final String TAG = this.getClass().getSimpleName();
    private final BehaviorSubject<ActivityEvent> mLifecycleSubject = BehaviorSubject.create();
    private Cache<String, Object> mCache;
    private Unbinder mUnbinder;

    private LoadingDialog mLoadingDialog;



    @Inject
    @Nullable
    protected P mPresenter;//如果当前页面逻辑简单, Presenter 可以为 null

    @NonNull
    @Override
    public synchronized Cache<String, Object> provideCache() {
        if (mCache == null) {
            mCache = ArmsUtils.obtainAppComponentFromContext(this).cacheFactory().build(CacheType.ACTIVITY_CACHE);
        }
        return mCache;
    }

    @NonNull
    @Override
    public final Subject<ActivityEvent> provideLifecycleSubject() {
        return mLifecycleSubject;
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        View view = convertAutoView(name, context, attrs);
        return view == null ? super.onCreateView(name, context, attrs) : view;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (useLandscape()) {
            ScreenUtils.setLandscape(this);
        }
        super.onCreate(savedInstanceState);
        try {
            int layoutResID = initView(savedInstanceState);
            //如果initView返回0,框架则不会调用setContentView(),当然也不会 Bind ButterKnife
            if (layoutResID != 0) {
                setContentView(layoutResID);
                //绑定到butterknife
                mUnbinder = ButterKnife.bind(this);
            }
        } catch (Exception e) {
            if (e instanceof InflateException) throw e;
            e.printStackTrace();
        }
        initData(savedInstanceState);
        if(useNetworkStatus() && !NetworkUtils.isRegisteredNetworkStatusChangedListener(this)){
            NetworkUtils.registerNetworkStatusChangedListener(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(useNetworkStatus() && NetworkUtils.isRegisteredNetworkStatusChangedListener(this)){
            NetworkUtils.unregisterNetworkStatusChangedListener(this);
        }
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY)
            mUnbinder.unbind();
        this.mUnbinder = null;
        if (mPresenter != null)
            mPresenter.onDestroy();//释放资源
        this.mPresenter = null;
    }


    @Override
    public boolean useEventBus() {
        return true;
    }


    @Override
    public boolean useFragment() {
        return true;
    }


    /**
     * 是否为横屏
     *
     * @return
     */
    protected boolean useLandscape() {
        return true;
    }


    /**
     * 是否注册网络状态监听
     * @return
     */
    protected boolean useNetworkStatus() { return true; }

    protected void setTitle(TextView tvTitle,String title){
        if(tvTitle == null || TextUtils.isEmpty(title)) return;
        AssetManager manager = getAssets();
        Typeface tf = Typeface.createFromAsset(manager, "fonts/lianmeng.ttf");
        tvTitle.setText(title);
        tvTitle.setTypeface(tf);
    }


    protected void showBaseLoading() {
        if(mLoadingDialog == null){
            mLoadingDialog = new LoadingDialog(this);
        }
        if(!mLoadingDialog.isShowing()){
            mLoadingDialog.show();
        }
    }

    protected void hideBaseLoading() {
        if(mLoadingDialog != null && mLoadingDialog.isShowing()){
            mLoadingDialog.cancel();
        }
    }


    @Override
    public void onDisconnected() {
        onNetworkDisconnected();
    }

    @Override
    public void onConnected(NetworkUtils.NetworkType networkType) {
        onNetworkConnected(networkType,NetworkUtils.getSSID());
    }


    /**
     * 网络连接断开回调
     */
    protected void onNetworkDisconnected(){}


    /**
     * 网络状态改变时回调
     * @param networkType
     * @param ssid
     */
    protected void onNetworkConnected(NetworkUtils.NetworkType networkType,String ssid){ }



}
